<template>
  <div>
    <div class="node" v-has="{action: 'charge:wx:list', effect: 'move'}">
    <div :class="[$style.search_box, $style.topSelect]">
      <SearchInput
        placeholder="订单编号"
        label="订单编号"
        style="margin-right:20px"
        :value="oid"
        :onChange="(val) => changeSearch('oid',val)"
        :onBlur="getRate"
      />
      <div :class="$style.selectBox">
        <label>订单状态：</label>
        <el-select
          v-model="state"
          clearable
          @change="getRate"
          placeholder="请选择状态"
        >
          <el-option label="待支付" :value="1"></el-option>
          <el-option label="已支付" :value="2"></el-option>
          <el-option label="已关闭" :value="3"></el-option>
        </el-select>
      </div>
       <div :class="$style.selectBox" >
        <label>支付时间：</label>
        <el-date-picker
          v-model="appointmentTime"
          type="daterange"
          @change="getRate"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="timestamp"
          :picker-options="pickerOptions"
          style="height:40px"
        ></el-date-picker>
      </div>
      <div :class="$style.searchBox1" style="margin-left: 20px">
        <el-button size="small" plain  icon="el-icon-search" @click="getRate">查询</el-button>        
      </div>
    </div>
    <Panel title="微信充值订单" :intro="intro">
      <!-- <div slot='ext' :class="$style.examine">
        <div class="adopt" @click="()=>editStore()">新增</div>
    </div> -->
    <TableEncapsulation
      slot="body"
      :tableConfig="tableConfig"
      :tableData="tableData"
    >
    <template slot="operation" slot-scope="operation">
          <el-button
            type="text"
            @click="()=>commentDetail(operation.row.oid)"
          >查看</el-button>
          <el-button
          v-if="operation.row.state==2"
            type="text"
            @click="()=>remove(operation.row.id)"
          >停止充电</el-button>
        </template>
        <TablePager
          slot="pager"
          v-if="pager.use"
          :size="pager.size"
          :pageChange="pageChange"
          :show-total="pager.showTotal"
          :total="pager.total"
          :current="pager.current"
        >
          <span class="demonstration">GO></span>
        </TablePager>
    </TableEncapsulation>
    </Panel>
    </div>
    <div style="display: none;width:100%;height:100%;margin: auto;" v-has="{action: 'charge:wx:list', effect: 'noPremiss'}">
      <NoPremission></NoPremission>
    </div>
  </div>
</template>
<script>
import request from "@/tools/request";
import Panel from "@/components/Panel";
import TableEncapsulation from "@/components/TableEncapsulation";
import TablePager from "@/components/Pager";
import SearchInput from "@/components/SearchInput";
import { objectFilter } from "@/tools/array";
import { dateFormat } from "@/tools/time";
export default {
  name: "orderWxCharge",
  components: {
    TableEncapsulation,
    TablePager,
    Panel,
    SearchInput
  },
  data() {
    return {
      intro:{
        title:'微信充值订单-介绍文档',
        content: `<ul><li>小程序用户充值订单</li></ul><p><br></p>`
      },
      tableData: [],
       operation: true,
      query: {
        page: 1
      },
       pager: {
        use: true,
        total: 0,
        current: 1,
        size: 10,
        showTotal: true
      },
      oid:'',
      state:'',
      appointmentTime: [new Date().getTime() - 3600 * 1000 * 24 * 90, new Date().getTime()],
      pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
    };
  },
  created() {
    this.getRate();
  },
  computed: {
    tableConfig () {
      return {
        selection: true,
        columnConfig: [
          {
            label: "订单编号",
            type: "text",
            prop: "oid",
            align: "center"
          },
          {
            label: "用户",
            type: "text",
            prop: "UName",
            align: "center"
          },
          {
            label: "用户ID",
            type: "text",
            prop: "uid",
            align: "center"
          },
          {
            label: "充值金额",
            type: "text",
            prop: "price",
            align: "center"
          },
          {
            label: "支付状态",
            type: "other",
            prop: "state",
            align: "center",
            fnc: val => val == 1 ? "待支付" : val == 2 ? "已支付" :"已关闭"
          },
          {
            label: "已退款金额",
            type: "text",
            prop: "refunded",
            align: "center",
          },
          {
            label: "微信订单编号",
            type: "text",
            prop: "wxSn",
            align: "center"
          },
          
          {
            label: "支付时间",
            type: "text",
            prop: "payTime",
            align: "center"
          },
        //   this.operation && {
        //     type: "operation",
        //     align: "center",
        //     label: "操作",
        //     width:180
        //   }
        ].filter(Boolean)
        }
    }
  },
  methods: {
    async getRate() {
      let endTime = this.appointmentTime ? (dateFormat(this.appointmentTime[1] + 86400000-1, "yyyy-MM-dd HH:mm:ss")) : undefined
      let beginTime = this.appointmentTime ? (dateFormat(this.appointmentTime[0],"yyyy-MM-dd HH:mm:ss")) : undefined
      let state =  this.state
      let oid = this.oid
      let page = this.pager.current
      let search = objectFilter({beginTime, endTime, state, oid, page})
      const res = await request({
        url: "/charge/wx/list",
        method: "POST",
        data: search
      });
      this.tableData = res.page.list
      this.pager.total = res.page.totalCount
      this.pager.current = res.page.currPage
    },
    pageChange(val) {
      this.pager.current = val;
      this.getRate();
    },
    changeSearch(key, val) {
      this[key] = val;
      this.pager.current = 1;
    },
    editStore(id){
      this.$router.push({ path: "/editStore",query:{id:id} })
    },
    commentDetail(id){
      this.$router.push({ path: "/order/wx/info",query:{id:id} })
    },
    remove(id){
       this.$confirm('确认删除吗', '操作确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then( async () => {
        await DELETE(`/store/${id}`,id)
        this.$message.success('删除成功')
        if(this.tableData.length===1 && this.pager.current>1)
          this.$set(this.pager,'current', this.pager.current-1)
        this.queryData()
      })
    },
  }
}
</script>
<style lang='scss' module>
.examine{
    display: flex;
    align-items: center;
    :global{
        div{
            color: #fff;
            padding: 10px;
            margin: 0 10px;
            border-radius: 2px;
            font-size: 14px;
            cursor: pointer;
            min-width: 60px;
            text-align: center;
        }
        .adopt{
                background: #409EFF;
        }
    }
}
.search_box {
  display: flex;
  align-items: center;
  flex-wrap: wrap;

  :global {
    .el-input {
      width: 150px;
    }
    .el-select {
      .el-input {
        width: 90px;
      }
    }
    .el-input__icon {
      line-height: 27px;
    }
    .el-range-separator{
      line-height: 26px;
    }
  }
}

.topSelect {
  background-color: white;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 20px;
}
.selectBox {
  margin: 4px 10px;
  :global {
    label {
      font-size: 14px;
      color: #666666;
    }
    .el-input__inner {
      height: 32px;
    }
    .el-input__suffix {
      line-height: 32px;
    }
    .el-date-editor--datetime{
      width: 200px;
    }
  }
}
</style>